﻿Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient

Public Class DepartamentoDB
    Public Shared Function GetDepartamentos() As DataSet
        Dim cmd As New SqlCommand("spGetDepartamentos", DB.Conexion)
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet

        cmd.CommandType = CommandType.StoredProcedure
        da.SelectCommand = cmd
        da.Fill(ds, "Departamentos")
        Return ds
    End Function
    Public Shared Sub InsertarDepartamento(ByVal departamento As String, ByVal id_jefe As Integer)
        Dim cmd As New SqlCommand("spInsertDepartamento", DB.Conexion)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@departamento", departamento)
        cmd.Parameters.AddWithValue("@id_jefe", id_jefe)
        cmd.Parameters.Add("@Error", SqlDbType.Int).Direction = ParameterDirection.Output
        cmd.Parameters.Add("@ErrorDesc", SqlDbType.VarChar, 200).Direction = ParameterDirection.Output
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
        If cmd.Parameters("@error").Value <> 0 Then
            Throw New ApplicationException(cmd.Parameters("@ErrorDesc").Value.ToString())
        End If
    End Sub
    Public Shared Sub EliminarDepartamento(ByVal id_Departamento As Integer)
        Dim cmd As New SqlCommand("spDeleteDepartamento", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_departamento", id_Departamento)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub

    Public Shared Sub ActualizarDepartamento(ByVal id_departamento As Integer, ByVal departamento As String, ByVal id_jefe As Integer)
        Dim cmd As New SqlCommand("spUpdateDepartamento", DB.Conexion)

        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@id_departamento", id_departamento)
        cmd.Parameters.AddWithValue("@departamento", departamento)
        cmd.Parameters.AddWithValue("@id_jefe", id_jefe)
        cmd.Connection.Open()
        cmd.ExecuteNonQuery()
        cmd.Connection.Close()
    End Sub
End Class
